#寻找小于n的所有质数(已做速度优化)
import matplotlib.pyplot as plt
import time, math
import sys
sys.path.append('C:\文档\\1-自动化办公模板')
from mypack import Mypool

def zhishu(n):
    zs=[2,3]
    i=5  # 从5开始找质数
    while i<=n:
        if i%6!=1 and i%6!=5:
            i+=2
            continue
        else:
            k = math.sqrt(i)
            for j in range(3,1+int(k),2):
                if i%j == 0:
                    break
            else:
                zs.append(i)
            i+=2
    return (zs,n)

if __name__ == '__main__':
    t1 = time.time()
    Y = [0, 1, 2, 2]
    max_n = 1000000
    print(max_n, '内有', len(zhishu(max_n)[0]), '个')
    print('耗时%.3f秒' % (time.time()-t1))
    # 遍历前n个数画图
    # res = Mypool.MyProcessPool(5, zhishu, range(5, max_n+1)).go()
    # for i in res:
    #     Y.append(len(list(filter(lambda x: True if x <= i[-1] else False, i[0]))))  # 比n小的质数个数
    # t2 = time.time()
    # print('计算耗时：', round(t2 - t1, 2), 's')
    # print('查找范围', max_n, '以内')
    # print('质数个数=', Y[-1])
    # print(list(range(1, max_n+1)))
    # print(Y)
    # plt.scatter(range(1, max_n+1), Y)
    # plt.show()